//
//  PDetailController.swift
//  CJPack
//
//  Created by 陈警卫 on 2023-06-02.
//

import UIKit

class PDetailCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var titleL: UILabel!
    
    func setupView() {
        
        titleL = UILabel()
        titleL.textColor = .cj_colorRandom
        contentView.addSubview(titleL)
        titleL.snp.makeConstraints { make in
            make.center.equalTo(self)
        }
    }
}

class PDetailController: CJBaseController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupView()
        setupData()
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    var collectionView: UICollectionView!
    var canScroll: Bool = false
    
    func setupView() {
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        
        collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.cj_width, height: view.cj_height), collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        view.addSubview(collectionView)
        
        collectionView.register(PDetailCell.self, forCellWithReuseIdentifier: "PDetailCell")
        
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        collectionView.frame = CGRect(x: 0, y: 0, width: view.cj_width, height: view.cj_height)
    }
    
    func setupData() {
        NotificationCenter.default.addObserver(self, selector: #selector(scrollMenuMainBottom(noti: )), name: .init("cj.menu.main.bottom"), object: nil)
    }
    
    @objc func scrollMenuMainBottom(noti: NSNotification) {
        canScroll = true
        var offset = self.collectionView.contentOffset
        offset.y += noti.object as? Double ?? 0
        self.collectionView.contentOffset = offset
    }
    
}

extension PDetailController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if !canScroll {
            scrollView.contentOffset = .zero
        }
        if scrollView.contentOffset.y <= 0 {
            canScroll = false
            scrollView.contentOffset = .zero
            NotificationCenter.default.post(name: .init("cj.menu.sub.top"), object: nil)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PDetailCell", for: indexPath)
        if let cell = cell as? PDetailCell {
            cell.titleL.text = String(indexPath.row)
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.cj_width, height: 100)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
}

